package logic

import (
	"cart_srv/models"
	"context"

	"cart_srv/cart"
	"cart_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGoodsLogic {
	return &AddGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddGoodsLogic) AddGoods(in *cart.AddGoodsRequest) (*cart.AddGoodsResponse, error) {
	// todo: add your logic here and delete this line

	var g models.Goods
	g.Title = in.Title
	g.Image = in.Image
	g.Price = float64(in.Price)
	g.Stock = int(in.Stock)
	g.Status = int(in.Status)
	g.UserId = int(in.UserId)
	err := g.AddGoods()
	if err != nil {
		logx.Error("商品发布失败:", err)
		return nil, err
	}

	return &cart.AddGoodsResponse{GoodsId: int64(g.ID)}, nil
}
